import Mock from 'mockjs'


import { param2Obj } from '../utils/myUtils'
var Arr = []
for (var i = 0; i < 200; i++) {
    Arr.push({
        id: Mock.mock('@id'),
        name: Mock.mock('@cname'),
        jiuka: Mock.mock('@increment(1000)'),
        card: Mock.mock('@natural'),
        number: '17098530203',
        dwork: '口腔',
        testfee: Mock.Random.natural(30, 30),
        state: '良好',
        date: Mock.mock('@date'),
        time: Mock.mock('@time'),
    })
}










export default {
    na: config => {
        console.log('config', param2Obj(config.url));
        const { search, page, pageSize } = param2Obj(config.url)
        var res = []
        if (search) {
            res = Arr.filter(item => {
                return item.name.indexOf(search) !== -1
            })
        } else {
            res = Arr
        }


        var newRes = res.filter((item, index) => {
            return (page - 1) * pageSize <= index && index < page * pageSize
        })

        return {
            code: 200,
            msg: '登录成功',
            data: newRes,
            total: res.length
        }
    },
    deletData: config => {
        console.log('config', config.body);
        const { jiuka } = JSON.parse(config.body)
            // console.log(card);
        Arr.forEach((item, index) => {
            if (item.card == jiuka) {
                Arr.splice(index, 1)
            }
        })
        return {
            code: 200,
            msg: '删除成功',
            data: null
        }
    },
    batchdelete: config => {
        const { ids } = JSON.parse(config.body)
        var res = Arr.filter(item => {
            return ids.indexOf(item.card) == -1
        })
        Arr = res
        return {
            code: 200,
            msg: '批量删除成功',
            data: null
        }
    },
    addHomeData: config => {
        console.log(JSON.parse(config.body));
        const { username, jiuka, card, number, dwork, testfee, state, date, } = JSON.parse(config.body)
        Arr.unshift({
            name: username,
            jiuka: jiuka,
            card: card,
            number: number,
            dwork: dwork,
            testfee: testfee,
            state: state,
            date: date,
        })
        console.log(JSON.parse(config.body));
        return {
            code: 200,
            msg: '添加成功',
            data: null
        }
    },
    updatahomeData: config => {
        const { id, username, jiuka, card, number, dwork, testfee, state, date, } = JSON.parse(config.body)
        Arr.forEach(item => {
            if (item.id == id) {
                item.name = username
                item.jiuka = jiuka
                item.card = card
                item.number = number
                item.dwork = dwork
                item.testfee = testfee
                item.state = state
                item.date = date

            }
        })

        return {
            code: 200,
            msg: '修改成功',
            data: null
        }
    }
}